import { createI18n } from 'vue-i18n'
import langZh from './lang/zh'
import langEn from './lang/en'
import vxeZh from 'vxe-pc-ui/lib/language/zh-CN'
import vxeEn from 'vxe-pc-ui/lib/language/en-US'
import eleZh from 'element-plus/es/locale/lang/zh-cn'
import eleEn from 'element-plus/es/locale/lang/en'

export const i18n = createI18n({
  legacy: false,
  globalInjection: true,
  silentTranslationWarn: true,
  sync: true,
  missingWarn: false,
  silentFallbackWarn: true,
  fallbackLocale: 'en-US',
  locale: localStorage.getItem('language'),
  messages: {
    'zh-CN': {
      ...langZh,
      ...vxeZh
    },
    'en-US': {
      ...langEn,
      ...vxeEn
    }
  }
})

export const $t = i18n.global.t

export const elementLocale = {
  'zh-CN': eleZh,
  'en-US': eleEn
}

export const updateI18nMessages = newLanguageData => {
  if (newLanguageData) {
    Object.keys(newLanguageData).forEach(locale => {
      if (i18n.global.messages.value[locale]) {
        i18n.global.mergeLocaleMessage(locale, newLanguageData[locale])
      }
    })
  }
}
